package fmtlog

import (
	"fmt"
	"gitee.com/zhongguo168a/gocodes/myx/logx/iface"
	"os"
)

func NewLogger() (obj *Logger) {
	obj = &Logger{
		Level: InfoLevel,
	}
	return
}

type Logger struct {
	Name string

	Data map[string]interface{}

	Level Level
}

func (l *Logger) printData() {
	if l.Data != nil {
		fmt.Printf(", %+v\n", l.Data)
	} else {
		fmt.Printf("\n")
	}

}

func (l *Logger) Ok(a ...interface{}) {
	if l.Level < InfoLevel {
		return
	}

	fmt.Print("[ok   ] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Fail(a ...interface{}) {
	if l.Level < InfoLevel {
		return
	}

	fmt.Print("[fail ] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Msg(a ...interface{}) {
	if l.Level < InfoLevel {
		return
	}

	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Debug(a ...interface{}) {
	if l.Level < DebugLevel {
		return
	}
	fmt.Print("[debug] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Info(a ...interface{}) {
	if l.Level < InfoLevel {
		return
	}

	fmt.Print("[info ] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Warn(a ...interface{}) {
	if l.Level < WarnLevel {
		return
	}
	fmt.Print("[warn ] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Error(a ...interface{}) {
	if l.Level < ErrorLevel {
		return
	}

	fmt.Print("[error] ")
	fmt.Print(a...)
	l.printData()
}

func (l *Logger) Fatal(a ...interface{}) {
	if l.Level < FatalLevel {
		return
	}

	fmt.Print("[fatal] ")
	fmt.Print(a...)
	l.printData()
	os.Exit(-1)
}

func (l *Logger) With(m map[string]interface{}) iface.ILogger {
	return &Logger{
		Data: m,
	}
}
